<template>
  <div class="com-contatiner">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      chertInstane: null,
      allData: '',
      startValue: 0,
      endValue: 9,
      timeId: ''
    }
  },
  created () {
    this.$socket.registerCallBack('ranktData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.startInterval()
    this.chertInstane.on('mousemove', () => {
      console.log(1)
      clearInterval(this.timeId)
    })
    this.chertInstane.on('mouseout', () => {
      this.startInterval()
    })
    this.$socket.send({
      action: 'getData',
      socketType: 'ranktData',
      chartName: 'rank',
      value: ''
    })
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timeId)
    this.$socket.unRegisterCallBack('ranktData')
  },
  methods: {
    initChart () {
      this.chertInstane = this.$echarts.init(this.$refs.rank_ref, this.theme)
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      // 初始化echarts的数据
      const initOption = {
        title: {
          text: '▍ 地区销售排行榜',
          left: 20,
          top: 20
        },
        grid: {
          top: '5%',
          right: '5%',
          // eslint-disable-next-line no-dupe-keys
          top: '40%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          show: true // 默认就是true 可写可不写
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar',
            itemStyle: {
              color: arg => {
                let targetColorArr = null
                if (arg.value > 300) {
                  targetColorArr = colorArr[0]
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: targetColorArr[0]
                    }, // 0的颜色值
                    {
                      offset: 1,
                      color: targetColorArr[1]
                    } // 100的颜色值
                  ])
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1]
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: targetColorArr[0]
                    }, // 0的颜色值
                    {
                      offset: 1,
                      color: targetColorArr[1]
                    } // 100的颜色值
                  ])
                } else {
                  targetColorArr = colorArr[2]
                  return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 0,
                      color: targetColorArr[0]
                    }, // 0的颜色值
                    {
                      offset: 1,
                      color: targetColorArr[1]
                    } // 100的颜色值
                  ])
                }
              }
            }
          }
        ]
      }
      // 展示
      this.chertInstane.setOption(initOption)
    },
    // 获取服务器数据
    getData (res) {
      // const { data: res } = await this.$http.get('rank')
      this.allData = res
      this.allData = this.allData.sort((a, b) => {
        return b.value - a.value
      })
      // 调用updataChart方法
      this.updataChart()
    },
    // 处理数据
    updataChart () {
      const nameData = this.allData.map(val => {
        return val.name
      })
      const valueData = this.allData.map(val => {
        return val.value
      })
      const dataOption = {
        xAxis: {
          data: nameData
        },
        series: [
          {
            data: valueData
          }
        ]
      }
      this.chertInstane.setOption(dataOption)
    },
    screenAdapter () {
      const titleFontSize = this.$refs.rank_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              borderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.chertInstane.setOption(adapterOption)
      this.chertInstane.resize()
    },
    startInterval () {
      if (this.timeId) {
        clearInterval(this.timeId)
      }
      this.timeId = setInterval(() => {
        if (this.allData.length - 1 < this.endValue) {
          this.startValue = 0
          this.endValue = 9
        }
        this.startValue++
        this.endValue++
        this.initChart()
      }, 3000)
    }
  },
  computed: {
    // eslint-disable-next-line no-undef
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chertInstane.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updataChart() // 更新图表的展示
    }
  }
}
</script>

<style>
</style>
